package org.feuyeux.loom;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

@Slf4j
public class WispSample {

    private static void thread() {
        try {
            ExecutorService e = Executors.newCachedThreadPool();
            BlockingQueue<Byte> q1 = new LinkedBlockingQueue<>(), q2 = new LinkedBlockingQueue<>();
            e.submit(() -> pingPong(q2, q1));
            Future<?> f = e.submit(() -> pingPong(q1, q2));
            q1.put((byte) 1);
            log.info("thread: {} ms", f.get());
        } catch (Exception e) {
            log.error("", e);
        }
    }

    private static long pingPong(BlockingQueue<Byte> in, BlockingQueue<Byte> out) throws Exception {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1_000_000; i++)
            out.put(in.take());
        return System.currentTimeMillis() - start;
    }

    public static void main(String[] args) {
        WispSample.thread();
    }
}
